package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.service.IPublishService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author dxx
 * @since 2022-09-24
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    IApArticleContentService contentService;

    @Autowired
    @Lazy
    private IPublishService publishService;

    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {

        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(dto, article);
        // 判断是否存在id
        if (dto.getId() == null) {
            // 不存在id,新增文章,新增文章内容
            article.setCreatedTime(new Date());
            this.save(article);
            // 保存文章内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(article.getId());
            content.setContent(dto.getContent());
            contentService.save(content);

            // 创建文章的静态页 使用异步线程来执行
            publishService.createHtml(article, dto.getContent());

            return ResponseResult.okResult(article.getId());
        }

        // 存在id,修改文章,修改文章内容
        this.updateById(article);
        // 修改文章内容
        LambdaUpdateWrapper<ApArticleContent> update = new LambdaUpdateWrapper<>();
        // update ap_article_content set content = ? where article_id = ?
        update.eq(ApArticleContent::getArticleId, dto.getId());
        update.set(ApArticleContent::getContent, dto.getContent());
        contentService.update(update);
        return ResponseResult.okResult(article.getId());
    }

    @Override
    public ResponseResult load(ArticleHomeDto dto, int type) {

        // 定义分页条件
        IPage<ApArticle> page = new Page<>(1, dto.getSize());
        // 定义查询条件
        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();
        // 如果频道ID不为0 ,添加频道id查询
        if (dto.getChannelId() != 0) {
            query.eq(ApArticle::getChannelId, dto.getChannelId());
        }
        // 如果是加载更多文章,需要判断文章的发布时间小于前端传递的最小时间
        if (type == 1) {
            query.lt(ApArticle::getPublishTime, dto.getMinTime());
        } else {
            // 如果是加载最新文章,需要判断文章的发布时间大于前端传递的最大时间
            query.gt(ApArticle::getPublishTime, dto.getMaxTime());
        }
        // 文章的状态不能是已下架和已删除
        query.eq(ApArticle::getIsDelete, false);
        query.eq(ApArticle::getIsDown, false);
        // 文章按照发布时间倒序排列
        query.orderByDesc(ApArticle::getPublishTime);
        // 分页查询
        IPage<ApArticle> iPage = this.page(page, query);
        return ResponseResult.okResult(iPage.getRecords());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        // 根据id查询文章表和文章内容表
        ApArticle apArticle = this.getById(dto.getArticleId());
        LambdaQueryWrapper<ApArticleContent> query = new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent articleContent = contentService.getOne(query);
        // 构建响应对象
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", articleContent);
        return ResponseResult.okResult(map);
    }
}

